package screens
{
	import com.greensock.TweenLite;
	
	import events.NavigationEvent;
	
	import starling.display.Button;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	
	public class Welcome extends Sprite
	{
		
		private var bg:Image;
		private var title:Image;
		private var hero:Image;
		private var playBtn:Button;
		private var aboutBtn:Button;
		
		public function Welcome()
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler);
		}
		
		private function onAddedToStageHandler(e:Event):void
		{
			drawScreen();
		}
		
		private function drawScreen():void
		{
			bg=new Image(Assets.getTexture("BgWelcome"));
			this.addChild(bg);
			
			title=new Image(Assets.getAtlas().getTexture("welcome_title"));
			title.x=440;
			title.y=20;
			this.addChild(title);
			
			hero=new Image(Assets.getAtlas().getTexture("welcome_hero"));
			this.addChild(hero);
			
			playBtn=new Button(Assets.getAtlas().getTexture("welcome_playButton"));
			this.addChild(playBtn);
			playBtn.x=500;
			playBtn.y=260;
			
			aboutBtn=new Button(Assets.getAtlas().getTexture("welcome_aboutButton"));
			this.addChild(aboutBtn);
			aboutBtn.x=410;
			aboutBtn.y=380;
			this.addEventListener(Event.TRIGGERED,onMainMenuClickHandler);
		}
		
		private function onMainMenuClickHandler(event:Event):void
		{
			var buttonClicked:Button=event.target as Button;
			if(buttonClicked==playBtn)
			{
				trace("button play clicked...");
				this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN,{id:"play"},true));
			}
		}
		
		public function initialize():void
		{
			this.visible=true;
			
			hero.x=-hero.width;
			hero.y=100;
			
			TweenLite.to(hero,2,{x:80});
			
			this.addEventListener(Event.ENTER_FRAME,onHeroAnimationHandler);
		}
		
		private function onHeroAnimationHandler(e:Event):void
		{
//			var currentDate:Date=new Date();	
			
//			hero.y=100+(Math.cos(currentDate.time*0.002)*25)；
//			playBtn.y=100+(Math.cos(currentDate.time*0.002)*25)；
//			aboutBtn.y=100+(Math.cos(currentDate.time*0.002)*25)；
//			hero.y=100+(Math.cos(currentDate.seconds*0.02)*25);
			var currentDate:Date = new Date();
			hero.y = 100 + (Math.cos(currentDate.getTime() * 0.002) * 25);
			playBtn.y = 260 + (Math.cos(currentDate.getTime() * 0.002) * 10);
			aboutBtn.y = 380 + (Math.cos(currentDate.getTime() * 0.002) * 10);
		}
		
		public function disposeTemporarily():void
		{
			// TODO Auto Generated method stub
			this.visible=false;
			if(this.hasEventListener(Event.ENTER_FRAME))
			{
				this.removeEventListener(Event.ENTER_FRAME,onHeroAnimationHandler);
			}
		}
	}
}